package controller;

import entity.Professor;
import entity.Student;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import manager.ProfessorManager;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import manager.StudentManager;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import util.Debug;

public class StudentController extends SimpleFormController {

    private ProfessorManager professorManager;
    private List<Professor> listProfessors;
    private StudentManager studentManager;

    
public StudentController() {
        setCommandClass(Student.class);
        setCommandName("studentForm");
    }
    @Override
    protected Map referenceData(HttpServletRequest request) throws Exception {
        Map referenceData = new HashMap();
        listProfessors = professorManager.getListProfessors();
        referenceData.put("professorList", listProfessors);
        return referenceData;
    }

    public void setProfessorManager(ProfessorManager professorManager) {
        this.professorManager = professorManager;
    }

    public ProfessorManager getProfessorManager() {
        return professorManager;
    }

    @Override
    protected ModelAndView onSubmit(HttpServletRequest request,
            HttpServletResponse response, Object command, BindException errors)
            throws Exception {
        List<Professor> examiners = new LinkedList<Professor>();
        Student s = (Student) command;
        Professor supervisor = findProfessor(s.getSupervisorId());
        Debug.d(s.getName());
        Professor exm1 = findProfessor(s.getExaminerId1());
        Professor exm2 = findProfessor(s.getExaminerId2());
        exm1.setExaminerId(1);
        exm2.setExaminerId(2);
        examiners.add(exm1);
        examiners.add(exm2);
        Professor president = findProfessor(s.getPresidentId());
        Professor secretary = findProfessor(s.getSecretaryId());
        Professor additionalMember = findProfessor(s.getAdditionalMemberId());
        s.setSupervisor(supervisor);
        s.setExaminerList(examiners);
        s.setPresident(president);
        s.setSecretary(secretary);
        s.setAdditionalMember(additionalMember);
        s.setExaminer1(exm1);
        s.setExaminer2(exm2);
        this.studentManager.insertNewStudent(s);
        return new ModelAndView("StudentSucess", "student", s);

    }

  
    

    public Professor findProfessor(int id) {
        Professor p = null;
        for (int i = 0; i < listProfessors.size(); i++) {
            if (listProfessors.get(i).getId() == id) {
                p = listProfessors.get(i);
                break;
            }
        }
        return p;
    }

    public void setStudentManager(StudentManager studentManager) {
        this.studentManager = studentManager;
    }

    public StudentManager getStudentManager() {
        return studentManager;
    }
}
